<?php

class action_loger
{
	private $table;
	private $id_prefixes = array(
		"cms_articles" => "art",
		"cms_categories" => "cat"
	);
	private $mods_columns = array(
		"cms_articles" => "category, heading, perex, content",
		"cms_categories" => "name, note"
	);
	private $sql;

	public function __construct($table)
	{
		$this->table = $table;
		$this->sql = new sqlquery;
	}

	private function lastID($fromLog = false)
	{
		$fromLog == true ? $table = $this->table."_log" : $table = $this->table;
		$this->sql->set_table($table);
		$this->sql->set_columns("id");
		$this->sql->set_orderby("id DESC");
		$this->sql->set_limit("1");
		$result = $this->sql->select();
		if($result == true)
		{
			$row = mysql_fetch_array($result);
			return $row["id"];
		}
		else
		{
			return false;
		}
	}

	public function new_entry()
	{
		if($id = $this->lastID())
		{
			$this->sql->setnull();
			$this->sql->set_table($this->table."_log");
			$this->sql->set_columns("action, user, ".$this->id_prefixes[$this->table]."ID");
			$this->sql->set_values("'new', 1, ".$id);
			$result = $this->sql->insert();
			if($result == true)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}

	public function save_previous_data($id)
	{
		$this->sql->setnull();
		$this->sql->set_table($this->table."_log");
		$this->sql->set_columns("id");
		$this->sql->set_where($this->id_prefixes[$this->table]."ID = $id");
		$this->sql->set_orderby("id DESC");
		$result = $this->sql->select();
		$row = mysql_fetch_array($result);
		$log_id = $row["id"];

		$this->sql->setnull();
		$this->sql->set_table($this->table);
		$this->sql->set_columns($this->mods_columns[$this->table]);
		$this->sql->set_where("id = $id");
		$result = $this->sql->select();
		if($result == true)
		{
			$row = mysql_fetch_array($result);
			$mods_values = array(
				"cms_articles" => "'$row[category]', '$row[heading]', '$row[perex]', '$row[content]'",
				"cms_categories" => "'".@$row["name"]."', '".@$row["note"]."'",
			);
			$this->sql->setnull();
			$this->sql->set_table($this->table."_mods");
			$this->sql->set_columns("logID, ".$this->mods_columns[$this->table]);
			$this->sql->set_values("$log_id, ".$mods_values[$this->table]);
			$result = $this->sql->insert();
			if($result == true)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}

	public function edit($id)
	{
		$this->sql->setnull();
		$this->sql->set_table($this->table."_log");
		$this->sql->set_columns("action, user, ".$this->id_prefixes[$this->table]."ID");
		$this->sql->set_values("'edit', 1, ".$id);
		$result = $this->sql->insert();
		if($result == true)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	public function delete_last_action()
	{
		$table = $this->table."_log";
		$this->sql->setnull();
		$this->sql->set_table($this->table);
		$this->sql->set_where("id = (SELECT id FROM $table ORDER BY id DESC LIMIT 1)");
		$result = $this->sql->delete();
		if($result == true)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	public function delete($id)
	{
		$this->sql->setnull();
		$this->sql->set_table($this->table."_log");
		$this->sql->set_columns("action, user, ".$this->id_prefixes[$this->table]."ID");
		$this->sql->set_values("'delete', 1, ".$id);
		$result = $this->sql->insert();
		if($result == true)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}

?>